package com.beemans.common.ui.views.tabview

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import com.beemans.common.R
import com.beemans.common.ext.loadImage
import com.beemans.common.ext.pt2px
import com.beemans.common.ext.updateMarginLayoutParams

/**
 * @author tiamosu
 * @date 2020/5/11.
 */
class TabBarItem @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    companion object {
        const val TYPE_PIC_TEXT = 0
        const val TYPE_TEXT_PIC = 1
        const val TYPE_ONLY_PIC = 2
        const val TYPE_ONLY_TEXT = 3
    }

    /**
     * 普通状态时的图标
     */
    private var iconNormalResId = -1

    /**
     * 选中状态时的图标
     */
    private var iconSelectedResId = -1

    /**
     * 普通状态时的图标颜色
     */
    private var iconNormalColor = -1

    /**
     * 选中状态时的图标颜色
     */
    private var iconSelectedColor = -1

    /**
     * 图标控件宽度
     */
    private var iconWidth = 12

    /**
     * 图标控件高度
     */
    private var iconHeight = 12

    /**
     * 文本
     */
    private var text: String? = null

    /**
     * 文本字体大小
     */
    private var textSize = 12f

    /**
     * 普通状态时的文本颜色
     */
    private var textNormalColor = -1

    /**
     * 选中状态时的文本颜色
     */
    private var textSelectedColor = -1

    /**
     * 文本是否加粗
     */
    private var isTextBold = false

    /**
     * 图标与文本间的距离
     */
    private var drawablePadding = 0

    /**
     * 是否选中状态
     */
    private var isItemSelected = false

    /**
     * 当前Tab选中的下标
     */
    var tabPosition = -1

    /**
     * item类型（0：图文；1：文图；2：仅图片；3：仅文本）
     */
    private var itemType = TYPE_PIC_TEXT

    private var imgIcon: AppCompatImageView? = null
    private var tvText: AppCompatTextView? = null

    init {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.TabBarItem)
        iconNormalResId = ta.getResourceId(R.styleable.TabBarItem_tbi_iconNormal, -1)
        iconNormalColor = ta.getColor(R.styleable.TabBarItem_tbi_iconNormalColor, -1)
        iconSelectedResId = ta.getResourceId(R.styleable.TabBarItem_tbi_iconSelected, -1)
        iconSelectedColor = ta.getColor(R.styleable.TabBarItem_tbi_iconSelectedColor, -1)
        iconWidth = ta.getDimensionPixelSize(R.styleable.TabBarItem_tbi_iconWidth, 12.pt2px)
        iconHeight = ta.getDimensionPixelSize(R.styleable.TabBarItem_tbi_iconHeight, 12.pt2px)

        text = ta.getString(R.styleable.TabBarItem_tbi_text)
        textSize = ta.getDimensionPixelSize(R.styleable.TabBarItem_tbi_textSize, 12.pt2px).toFloat()
        textNormalColor = ta.getColor(R.styleable.TabBarItem_tbi_textNormalColor, Color.GRAY)
        textSelectedColor = ta.getColor(R.styleable.TabBarItem_tbi_textSelectedColor, Color.WHITE)
        isTextBold = ta.getBoolean(R.styleable.TabBarItem_tbi_textBold, false)

        drawablePadding =
            ta.getDimensionPixelSize(R.styleable.TabBarItem_tbi_drawablePadding, 0)
        isItemSelected = ta.getBoolean(R.styleable.TabBarItem_tbi_selected, false)

        itemType = ta.getInt(R.styleable.TabBarItem_tbi_type, TYPE_PIC_TEXT)

        ta.recycle()

        initView(context)
    }

    private fun initView(context: Context) {
        val containerLayout = LinearLayout(context).apply {
            orientation = LinearLayout.VERTICAL
            gravity = Gravity.CENTER
            layoutParams = LayoutParams(-2, -2).apply { gravity = Gravity.CENTER }
        }

        imgIcon = AppCompatImageView(context).also {
            it.layoutParams = LayoutParams(iconWidth, iconHeight)
            if (iconNormalResId != -1) {
                it.loadImage(iconNormalResId)
            }
        }

        tvText = AppCompatTextView(context).also {
            it.layoutParams = LayoutParams(-2, -2)
            it.text = text
            it.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize)

            if (textNormalColor != -1) {
                it.setTextColor(textNormalColor)
            }
            if (isTextBold) {
                it.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
            }
        }

        when (itemType) {
            TYPE_PIC_TEXT -> {
                containerLayout.addView(imgIcon)
                containerLayout.addView(tvText)
                tvText?.updateMarginLayoutParams {
                    topMargin = drawablePadding
                }
            }
            TYPE_TEXT_PIC -> {
                containerLayout.addView(tvText)
                containerLayout.addView(imgIcon)
                imgIcon?.updateMarginLayoutParams {
                    topMargin = drawablePadding
                }
            }
            TYPE_ONLY_PIC -> {
                containerLayout.addView(imgIcon)
            }
            TYPE_ONLY_TEXT -> {
                containerLayout.addView(tvText)
            }
        }

        addView(containerLayout)
        isSelected = isItemSelected
    }

    override fun setSelected(selected: Boolean) {
        setSelectedStatus(selected)
        super.setSelected(selected)
    }

    fun setSelectedStatus(selected: Boolean) {
        textChange(selected)
        imgChange(selected)
    }

    private fun textChange(selected: Boolean = isSelected) {
        if (selected) {
            tvText?.setTextColor(textSelectedColor)
        } else {
            tvText?.setTextColor(textNormalColor)
        }
    }

    private fun imgChange(selected: Boolean = isSelected) {
        if (selected) {
            if (iconSelectedResId != -1) {
                imgIcon?.loadImage(iconSelectedResId)
            }
            if (iconSelectedColor != -1) {
                imgIcon?.setColorFilter(iconSelectedColor)
            }
        } else {
            if (iconNormalResId != -1) {
                imgIcon?.loadImage(iconNormalResId)
            }
            if (iconNormalColor != -1) {
                imgIcon?.setColorFilter(iconNormalColor)
            }
        }
    }

    fun setImg(block: AppCompatImageView.() -> Unit): TabBarItem {
        imgIcon?.apply(block)
        return this
    }

    fun setImgIcon(
        @DrawableRes iconNormalResId: Int = -1,
        @DrawableRes iconSelectedResId: Int = -1
    ) {
        if (iconNormalResId != -1) {
            this.iconNormalResId = iconNormalResId
        }
        if (iconSelectedResId != -1) {
            this.iconSelectedResId = iconSelectedResId
        }
        imgChange()
    }

    fun setText(block: AppCompatTextView.() -> Unit): TabBarItem {
        tvText?.apply(block)
        return this
    }

    fun setTextColor(
        @ColorInt textNormalColor: Int = -1,
        @ColorInt textSelectedColor: Int = -1
    ) {
        if (textNormalColor != -1) {
            this.textNormalColor = textNormalColor
        }
        if (textSelectedColor != -1) {
            this.textSelectedColor = textSelectedColor
        }
        textChange()
    }
}
